package com.example.notification;

import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.NotificationCompat;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.RemoteViews;

public class MainActivity extends AppCompatActivity {

    private NotificationManager mNotificationManager;

    private String CHANNEL_ID = "100";
    private String CHANNEL_NAME = "MY_CHANNEL";
    private int NOTIFICATION_ID_1 = 1;
    private int NOTIFICATION_ID_2 = 2;
    private int NOTIFICATION_ID_3 = 3;
    private int NOTIFICATION_ID_4 = 4;
    private int NOTIFICATION_ID_5 = 5;
    private int NOTIFICATION_ID_6 = 6;
    private int NOTIFICATION_ID_7 = 7;

    private int test = 1;

    @RequiresApi(api = Build.VERSION_CODES.O)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

        //创建channel
        if (mNotificationManager.getNotificationChannel(CHANNEL_ID) == null) {
            NotificationChannel notificationChannel = new NotificationChannel(CHANNEL_ID, CHANNEL_NAME, mNotificationManager.IMPORTANCE_HIGH);
            notificationChannel.enableLights(true);
            notificationChannel.enableVibration(true);
            notificationChannel.setLightColor(Color.RED);
            notificationChannel.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
            notificationChannel.setShowBadge(true);
            notificationChannel.setBypassDnd(true);
            notificationChannel.setVibrationPattern(new long[]{100, 200, 300, 400});//这个是自定义振动,第一个参数是开始振动前等待的时间，第二个参数是第一次振动的时间，第三个参数是第二 ,次振动的时间，以此类推，随便定义多长的数组。但是采用这种方法，没有办法做到重复振动。
            //加入手机震动，一定要在manifest.xml中加入权限：
            //<uses-permission android:name="android.permission.VIBRATE" />
            //notificationChannel.setDescription(desc);
            mNotificationManager.createNotificationChannel(notificationChannel);
        }


        findViewById(R.id.btn1).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, MainActivity2.class);
                PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this, 1, intent, PendingIntent.FLAG_CANCEL_CURRENT);
                Notification.Builder builder = new Notification.Builder(MainActivity.this, CHANNEL_ID);
                builder.setSmallIcon(R.drawable.small_notification)//不设置会闪退
                        .setContentTitle("标题")
                        .setContentText("这里是内容。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。！！！")//后面多出的就看不见了
                        .setTicker(test + "通知来了上课回复顾客u机会让给i恶化她让我和4(⊙﹏⊙)五苦厄和日期              ")//设置状态栏的提示（重复发送，同id，如果内容不变，则不会刷新，内容不同才会弄一个新的
                        //.setTimeoutAfter(1000)//多少毫秒后自动消失(至少要ticker显示完
                        .setAutoCancel(true)//点击后自动删除通知
                        .setDefaults(Notification.DEFAULT_SOUND|Notification.DEFAULT_VIBRATE)//默认的振动和铃声
                        .setContentIntent(pendingIntent)//设置intent（打开另一个Activity
                        .build();//实测无用，删了也没影响
                //test++;
                mNotificationManager.notify(NOTIFICATION_ID_1, builder.build());//发送通知(第二个参数是Notification,如果是Builder，Builder.build()即是Notification
            }
        });


        findViewById(R.id.btn2).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mNotificationManager.cancel(NOTIFICATION_ID_1);
            }
        });


        findViewById(R.id.btn3).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {//目前并没有发现Notification.Builder和NotificationCompat.Builder有什么区别
                NotificationCompat.Builder builder = new NotificationCompat.Builder(MainActivity.this, CHANNEL_ID);
                builder.setSmallIcon(R.drawable.small_notification)//不设置会闪退
                        .setContentTitle("标题")
                        .setContentText("内容")
                        .setOngoing(true)//设置是否常驻,true为常驻(手动划不掉
                        .build();//实测无用，删了也没影响
                mNotificationManager.notify(NOTIFICATION_ID_2, builder.build());
            }
        });

        findViewById(R.id.btn4).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Notification.Builder builder = new Notification.Builder(MainActivity.this, CHANNEL_ID);
                builder.setSmallIcon(R.drawable.small_notification)
                        .setContentTitle("标题")
                        .setContentText("正在下载xxx")
                        .setOngoing(true)//不可手动消除通知
                        .setTicker("开始下载")
                        .build();

                //模拟下载
                new Thread(new Runnable() {
                    @Override
                    public void run() {

                        for (int progress = 0; progress < 100; progress +=5)  {
                            //将setProgress里的第三个参数设置为true可设置为无明确进度的进度条样式
                            builder.setProgress(100, progress, false);
                            mNotificationManager.notify(NOTIFICATION_ID_3, builder.build());
                            try {
                                Thread.sleep(1000);
                            } catch (InterruptedException e) {
                                System.out.println("sleep failure");
                            }
                        }

                        builder.setContentText("下载完成")
                                .setProgress(0,0,false)
                                .setOngoing(false);
                        mNotificationManager.notify(NOTIFICATION_ID_3, builder.build());
                    }
                }).start();

            }
        });


        findViewById(R.id.btn5).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, MainActivity2.class);
                PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this, 1, intent, PendingIntent.FLAG_CANCEL_CURRENT);
                RemoteViews notificationview = new RemoteViews(getPackageName(), R.layout.notification_layout);
                notificationview.setTextViewText(R.id.ntf_tv,"被设置过的text");
                notificationview.setOnClickPendingIntent(R.id.ntf_btn, pendingIntent);
                Notification.Builder builder = new Notification.Builder(MainActivity.this, CHANNEL_ID);
                builder.setSmallIcon(R.drawable.small_notification)
                        .setCustomContentView(notificationview)
                        .setOngoing(true)
                        .build();
                mNotificationManager.notify(NOTIFICATION_ID_4, builder.build());
            }
        });


        findViewById(R.id.btn6).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Notification.BigTextStyle bigTextStyle = new Notification.BigTextStyle();
                bigTextStyle.setSummaryText("内容摘要").setBigContentTitle("标题").bigText("展开后的内容");
                Notification.Builder builder = new Notification.Builder(MainActivity.this, CHANNEL_ID);
                builder.setSmallIcon(R.drawable.small_notification).setStyle(bigTextStyle).build();
                mNotificationManager.notify(NOTIFICATION_ID_5, builder.build());
            }
        });


        findViewById(R.id.btn7).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Notification.InboxStyle inboxStyle = new Notification.InboxStyle();
                inboxStyle.setBigContentTitle("标题").setSummaryText("内容摘要");
                for (int i = 0; i < 7; i++) {
                    inboxStyle.addLine("line" + i);
                }
                Notification.Builder builder = new Notification.Builder(MainActivity.this, CHANNEL_ID);
                builder.setSmallIcon(R.drawable.small_notification).setStyle(inboxStyle).build();
                mNotificationManager.notify(NOTIFICATION_ID_6, builder.build());
            }
        });

        findViewById(R.id.btn8).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Notification.BigPictureStyle bigPictureStyle = new Notification.BigPictureStyle();
                bigPictureStyle.bigPicture(BitmapFactory.decodeResource(MainActivity.this.getResources(), R.drawable.ic_launcher_background));
                Notification.Builder builder = new Notification.Builder(MainActivity.this, CHANNEL_ID);
                builder.setSmallIcon(R.drawable.small_notification).setStyle(bigPictureStyle).build();
                mNotificationManager.notify(NOTIFICATION_ID_7, builder.build());
            }
        });
    }
}